

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ab.util.AbSharedUtil;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.activity.SearchActivity;
import com.example.administrator.caigou51.adapter.MainFragmentAdapter;
import com.example.administrator.caigou51.fragment.SearchFragment;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.card.SearchCard;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.widget.WrapContentHeightViewPager;
import com.example.administrator.caigou51.widget.dialog.SweetDialogManager;
import com.example.administrator.caigou51.widget.viewpagerindicator.CirclePageIndicator;

import java.util.ArrayList;

import cn.pedant.SweetAlert.SweetAlertDialog;


public class SearchCardView extends CardItemView<SearchCard> implements View.OnClickListener{
    private Context mContext;
    private boolean isAdd = false;
    private View imageViewDel,textViewHistory;
    SearchCard searchCard;
    ArrayList<android.support.v4.app.Fragment> list ;
    ArrayList<android.support.v4.app.Fragment> listLast ;
    MainFragmentAdapter fragmentAdapter;
    MainFragmentAdapter fragmentAdapterLast;
    WrapContentHeightViewPager viewPager;
    CirclePageIndicator indicator;


    private com.example.administrator.caigou51.recyclerCard.basic.MaterialListView materialListView;
    private RelativeLayout relativeLayoutlast;
    private com.example.administrator.caigou51.widget.WrapContentHeightViewPager viewPagerlast;
    private com.example.administrator.caigou51.widget.viewpagerindicator.CirclePageIndicator indicatorlast;
    public SearchCardView(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public SearchCardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public SearchCardView(Context context,
                                    AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }
    @Override
    public void build(SearchCard card) {
        // TODO Auto-generated method stub
        super.build(card);
        searchCard=card;

            viewPager= (WrapContentHeightViewPager) findViewById(R.id.viewPager);
            indicator= (CirclePageIndicator) findViewById(R.id.indicator);indicator.setVisibility(View.GONE);
            imageViewDel=findViewById(R.id.imageViewDel);imageViewDel.setOnClickListener(this);



            relativeLayoutlast = (RelativeLayout) findViewById(R.id.relativeLayoutlast);
            viewPagerlast = (com.example.administrator.caigou51.widget.WrapContentHeightViewPager) findViewById(R.id.viewPagerlast);
            indicatorlast = (com.example.administrator.caigou51.widget.viewpagerindicator.CirclePageIndicator) findViewById(R.id.indicatorlast);
        indicatorlast.setVisibility(View.GONE);
            textViewHistory=findViewById(R.id.textViewHistory);

            //上面的标签
            list = new ArrayList<Fragment>();
            list.add(new SearchFragment(card.getTitleBeanList(),true));
        //    list.add(new SearchFragment(card.getTitleBeanList()));
            fragmentAdapter = new MainFragmentAdapter(((GBaseActivity)mContext).getSupportFragmentManager(), list);
            viewPager.setAdapter(fragmentAdapter);
            viewPager.setOffscreenPageLimit(2);
            indicator.setViewPager(viewPager);
            //下面的标签
            if (card.getTitleBeanLastList()!=null && card.getTitleBeanLastList().size()>0) {
                relativeLayoutlast.setVisibility(View.VISIBLE);
                textViewHistory.setVisibility(View.GONE);
                listLast = new ArrayList<Fragment>();
                listLast.add(new SearchFragment(card.getTitleBeanLastList(),false));
              //  listLast.add(new SearchFragment(card.getTitleBeanLastList()));

                fragmentAdapterLast = new MainFragmentAdapter(((GBaseActivity) mContext).getSupportFragmentManager(), listLast);
                viewPagerlast.setAdapter(fragmentAdapterLast);
                viewPagerlast.setOffscreenPageLimit(2);
                indicatorlast.setViewPager(viewPagerlast);
                imageViewDel.setVisibility(VISIBLE);
            }else
            {
                relativeLayoutlast.setVisibility(View.GONE);
                textViewHistory.setVisibility(View.VISIBLE);
                imageViewDel.setVisibility(GONE);
            }

        }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.imageViewDel :
                SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) mContext, "确认清空历史搜索吗?", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                    }
                }, new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        AbSharedUtil.putString(App.getInstance().getApplicationContext(), SearchActivity.SearchBeanList, "");
                        searchCard.getTitleBeanLastList().clear();
                        searchCard.getMaterialListView().getAdapter().notifyDataSetChanged();
                        ToastUtil.showToast("删除成功");
                        sweetAlertDialog.dismiss();
                    }
                },"取消","确定");
                break;
        }
    }


}
